package com.situ.studentmanagement.controller;

import com.situ.studentmanagement.Constants;
import com.situ.studentmanagement.dao.ClassMapper;
import com.situ.studentmanagement.model.ClassRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2016/7/15.
 */
@Controller
@RequestMapping("/class")
public class ClassAccountController {
    private  int modifyID;
    @Autowired
    private ClassMapper classMapper;

    @RequestMapping(value="/class_man",method = RequestMethod.GET)
    public String classMan(Model model){
        List<ClassRoom> classes = classMapper.getAllClassRoom();
        model.addAttribute("classes",classes);
        return "class_man";
    }
//导航到添加班级页面
    @RequestMapping(value="/add_class",method = RequestMethod.GET)
    public String addClass(){
        return "add_class";
    }

    //往数据库中添加班级
    @RequestMapping(value="/addClassToSql",method = RequestMethod.POST)
    public String addClassToSql(Model model,
                                HttpSession Session,
                                @RequestParam("name") String name,
                                @RequestParam("level") String level,
                                @RequestParam("school_name") String school_name){
            int AdminID= (int) Session.getAttribute(Constants.ADMIN_LOGIN_ATTR_ID);
            ClassRoom classRoom=new ClassRoom(0,name,level,school_name,AdminID);
            classMapper.addClass(classRoom);
        checkAllClass(model);
        return "class_man";
    }

    //在班级管理页面获取班级详细信息
    @RequestMapping(value="/checkAllClass")
    public String checkAllClass(Model model){
        List<ClassRoom> classes = classMapper.getAllClassRoom();
        model.addAttribute("classes",classes);
        return "class_man";
    }
    //导航到班级修改页面
    @RequestMapping(value="/modify_class")
    public String modifyClass( Model model ,
                               @RequestParam("modify_id")  int modify_id){
        modifyID=modify_id;
        ClassRoom classRoom=classMapper.getClassById(modifyID);
        model.addAttribute("classRoom",classRoom);
        return "modify_class";
    }

    //修改班级
    @RequestMapping(value="/modifyClass")
    public String modifyclass( Model model,
                               @RequestParam("name") String name,
                               @RequestParam("level") String level,
                               @RequestParam("school_name") String school_name){
        classMapper.setClassById(modifyID,name,level,school_name);
        List<ClassRoom> classes = classMapper.getAllClassRoom();
        model.addAttribute("classes",classes);
        return "class_man";
    }

}
